package com.example.projectmanagement.service.study;

import com.example.projectmanagement.model.study.Achievement;
import com.example.projectmanagement.model.User;
import com.example.projectmanagement.repository.study.AchievementRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class AchievementService {

    @Autowired
    private AchievementRepository achievementRepository;

    /**
     * 为用户解锁成就
     */
    @Transactional
    public Achievement unlockAchievement(User user, String achievementName, String achievementType, 
                                       String description, String badgeIcon) {
        // 检查用户是否已经获得该成就
        List<Achievement> existingAchievements = achievementRepository.findByUserAndAchievementType(user, achievementType);
        for (Achievement existing : existingAchievements) {
            if (existing.getAchievementName().equals(achievementName)) {
                return existing; // 用户已经获得该成就
            }
        }
        
        // 创建新的成就记录
        Achievement achievement = new Achievement();
        achievement.setUser(user);
        achievement.setAchievementName(achievementName);
        achievement.setAchievementType(achievementType);
        achievement.setDescription(description);
        achievement.setBadgeIcon(badgeIcon);
        achievement.setEarnedTime(LocalDateTime.now());
        
        return achievementRepository.save(achievement);
    }

    /**
     * 获取用户的所有成就
     */
    public List<Achievement> getUserAchievements(User user) {
        return achievementRepository.findByUser(user);
    }

    /**
     * 获取用户的成就（分页）
     */
    public Page<Achievement> getUserAchievements(User user, Pageable pageable) {
        return achievementRepository.findByUser(user, pageable);
    }

    /**
     * 获取用户特定类型的成就
     */
    public List<Achievement> getUserAchievementsByType(User user, String achievementType) {
        return achievementRepository.findByUserAndAchievementType(user, achievementType);
    }

    /**
     * 解锁学习类成就
     */
    @Transactional
    public Achievement unlockStudyAchievement(User user, String achievementName) {
        switch (achievementName) {
            case "LEARNING_NEWBIE":
                return unlockAchievement(user, "学习新手", "学习类", "完成首个课程", "beginner-badge.png");
            case "PERSISTENT_LEARNER":
                return unlockAchievement(user, "坚持达人", "学习类", "连续学习7天", "persistent-badge.png");
            case "STUDY_MASTER":
                return unlockAchievement(user, "学霸之王", "学习类", "累计学习时长≥100小时", "master-badge.png");
            default:
                throw new IllegalArgumentException("Unknown achievement: " + achievementName);
        }
    }

    /**
     * 解锁互动类成就
     */
    @Transactional
    public Achievement unlockInteractionAchievement(User user, String achievementName) {
        switch (achievementName) {
            case "CURIOUS_BABY":
                return unlockAchievement(user, "好奇宝宝", "互动类", "提问≥10次", "curious-badge.png");
            case "HELPFUL_TEACHER":
                return unlockAchievement(user, "热心导师", "互动类", "回答被采纳≥5次", "teacher-badge.png");
            default:
                throw new IllegalArgumentException("Unknown achievement: " + achievementName);
        }
    }

    /**
     * 解锁里程碑类成就
     */
    @Transactional
    public Achievement unlockMilestoneAchievement(User user, String achievementName) {
        switch (achievementName) {
            case "FIRST_STEPS":
                return unlockAchievement(user, "初出茅庐", "里程碑类", "完成首个课程", "first-steps-badge.png");
            case "MAKING_PROGRESS":
                return unlockAchievement(user, "小有所成", "里程碑类", "获得100积分", "progress-badge.png");
            case "LEVEL_UP":
                return unlockAchievement(user, "等级提升", "里程碑类", "达到Lv.5等级", "level-up-badge.png");
            default:
                throw new IllegalArgumentException("Unknown achievement: " + achievementName);
        }
    }
}